home *** CD-ROM | disk | FTP | other *** search
- Path: chaos.kulnet.kuleuven.ac.be!usenet
- From: Andreas De Troy <Andreas.DeTroy@ped.kuleuven.ac.be>
- Newsgroups: comp.lang.c++
- Subject: Re: What is referencing good for?
- Date: 1 Mar 1996 09:38:03 GMT
- Organization: KUL
- Message-ID: <4h6ghr$70a@chaos.kulnet.kuleuven.ac.be>
- NNTP-Posting-Host: pcip194.psy.kuleuven.ac.be
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.2N (Windows; I; 16bit)
-
- news:313539c9.4835024@news2.cts.com
-
- I think the ONLY reason why this referencing-thing is introduced in C++,
- is to allow operator overloading.
-
- Example: if you have a non-standard datatype and you would like to
- perform the "usual" arithmetic operations on it. Say you would like to
- add two complex numbers (suppose "complex" is a datatype defined by
- yourself), and you don't like to write it this way:
-
- complex a, b, c;
-
- a = add (b, c);
-
- With operator overloading, it could be written like this (don't know
- whether this is perfectly legal though):
-
- complex tmp, b, c, *a;
-
- a = &tmp;
-
- *a = b + c;
-
- But if you like to write it this way (the "natural" way):
-
- a = b + c; // a, b, and c not integers
-
- .. then you need to use referencing, otherwise <a> could not be modified
- in a statement like that.
-
- For the rest referencing is a rather dangerous thing and I would never
- recommend using it. People who say that it allows "cleaner" code are just
- missing the point. In "C" if you see something like:
-
- int a, b, c, d;
-
- swap (&a, &b);
- perform_thing (c, d);
-
- .. you know that a and b can be changed inside the function, and c and d
- not. This is very helpful for debugging. In C++ you don't know this, and
- you actually have to look at the exact function-definition, somewhere in
- a header, to make sure that c and d can be modified or not.
-
- So I would NEVER use the &-operator to produce so-called "cleaner" code.
-
-
-